package com.njupt.backtracking;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: wujiaming
 * @CreateTime: 2024/12/16 9:51
 * @Description: 78. 子集
 * @Version: 1.0
 */


public class Subsets_78 {

    public List<List<Integer>> subsets(int[] nums) {
        List<Integer> result = new LinkedList<>();
        List<List<Integer>> results = new ArrayList<>();
        int startIndex = 0;
        traceBacking(startIndex,nums,results,result);
        return results;
    }

    private void traceBacking(int startIndex, int[] nums, List<List<Integer>> results, List<Integer> result){
        results.add(new ArrayList(result));
        if(startIndex == nums.length){
            return;
        }

        for (int i = startIndex; i < nums.length; i++) {
            result.add(nums[i]);
            traceBacking(i+1,nums,results,result);
            result.remove(result.size()-1);
        }
    }

    public static void main(String[] args) {
        int[] nums = {1,2,3};
        Subsets_78 test = new Subsets_78();
        List<List<Integer>> results = test.subsets(nums);
        System.out.println(results);
    }
}
